package com.ss.isc.data.resource.service;

import com.ss.isc.data.baseinfo.common.model.User;
import com.ss.isc.data.baseinfo.service.BaseServiceImpl;
import com.ss.isc.data.resource.client.IHomePageResourceService;
import com.ss.isc.data.resource.common.dto.CountDTO;
import com.ss.isc.data.resource.common.model.AccessDevice;
import com.ss.isc.data.resource.common.model.Company;
import com.ss.isc.data.resource.common.model.Region;
import com.ss.isc.data.resource.common.model.Tollgate;
import com.ss.isc.data.resource.common.model.Village;
import com.ss.isc.data.resource.common.model.Wifi;
import com.ss.isc.data.resource.mapper.AccessDeviceMapper;
import com.ss.isc.data.resource.mapper.CompanyDao;
import com.ss.isc.data.resource.mapper.RegionMapper;
import com.ss.isc.data.resource.mapper.ResourceCountMapper;
import com.ss.isc.data.resource.mapper.TollgateMapper;
import com.ss.isc.data.resource.mapper.VillageMapper;
import com.ss.isc.data.resource.mapper.WifiMapper;
import com.ss.isc.util.StringUtils;
import com.ss.isc.util.file.FilePropertiesUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;




















@Service
@Transactional(rollbackFor = {Exception.class})
public class HomepageResourceServiceImpl
  extends BaseServiceImpl
  implements IHomePageResourceService
{
  @Autowired
  private CompanyDao companyMapper;
  @Autowired
  private WifiMapper wifiMapper;
  @Autowired
  private TollgateMapper tollgateMapper;
  @Autowired
  private RegionMapper regionMapper;
  @Autowired
  private ResourceCountMapper resourceCountMapper;
  @Autowired
  private VillageMapper villageMapper;
  @Autowired
  private AccessDeviceMapper accessDeviceMapper;
  
  public List<Company> findCompany(Village village) {
    User user = new User();
    user.setUserId(village.getUserIds());
    Map<String, String> map = new HashMap<String, String>();
    map.put("dsf", dataScopeFilter(user).replace("t1.", ""));
    village.setSqlMap(map);
    
    List<Company> findAllCompanys = this.companyMapper.findAllCompanys(village);
    for (Company company : findAllCompanys) {
      
      if (StringUtils.isNotBlank(company.getCompanyPic()) && !company.getCompanyPic().contains("http")) {
        company.setCompanyPic(FilePropertiesUtil.getHttpUrl() + company.getCompanyPic());
      }
    } 
    
    return findAllCompanys;
  }


  
  public List<Wifi> findWifis(Village village) {
    User user = new User();
    user.setUserId(village.getUserIds());
    Map<String, String> map = new HashMap<String, String>();
    map.put("dsf", dataScopeFilter(user).replace("t1.", ""));
    village.setSqlMap(map);
    List<Wifi> findWifis = this.wifiMapper.findWifis(village);
    for (Wifi wifi : findWifis) {
      
      if (StringUtils.isNotBlank(wifi.getDevicePicUrl()) && !wifi.getDevicePicUrl().contains("http")) {
        wifi.setDevicePicUrl(FilePropertiesUtil.getHttpUrl() + wifi.getDevicePicUrl());
      }
    } 
    
    return this.wifiMapper.findWifis(village);
  }

  
  public List<Tollgate> findTollgates(Village village) {
    User user = new User();
    user.setUserId(village.getUserIds());
    Map<String, String> map = new HashMap<String, String>();
    map.put("dsf", dataScopeFilter(user).replace("t1.", ""));
    village.setSqlMap(map);
    List<Tollgate> findTollgates = this.tollgateMapper.findTollgates(village);
    for (Tollgate tollgate : findTollgates) {
      
      if (StringUtils.isNotBlank(tollgate.getTollgatePicUrl()) && !tollgate.getTollgatePicUrl().contains("http")) {
        tollgate.setTollgatePicUrl(FilePropertiesUtil.getHttpUrl() + tollgate.getTollgatePicUrl());
      }
    } 
    return findTollgates;
  }



  
  public List<Region> findRegion(String regionName) { return this.regionMapper.findRegion(regionName); }




  
  public List<Region> searchNextRegion(String regionCode) { return this.regionMapper.searchNextRegion(regionCode); }




  
  public int peopleCount(String villageCode) { return this.resourceCountMapper.peopleCount(villageCode); }




  
  public int companyCount(String villageCode) { return this.resourceCountMapper.companyCount(villageCode); }




  
  public int houseCount(String villageCode) { return this.resourceCountMapper.houseCount(villageCode); }




  
  public List<Village> getVillageInformation(Village village) { return this.villageMapper.findList(village); }


  
  public List<AccessDevice> findAccessDevice(Village village) {
    User user = new User();
    user.setUserId(village.getUserIds());
    Map<String, String> map = new HashMap<String, String>();
    map.put("dsf", dataScopeFilter(user).replace("t1.", ""));
    village.setSqlMap(map);
    return this.accessDeviceMapper.findAccessDevice(village);
  }



  
  public List<CountDTO> everyPeopleCount() { return this.resourceCountMapper.everyPeopleCount(); }




  
  public CountDTO everyCompanyCount(String string) { return this.resourceCountMapper.everyCompanyCount(string); }




  
  public CountDTO everyHouseCount(String string) { return this.resourceCountMapper.everyHouseCount(string); }
}
